#!/usr/bin/perl -w
use LWP::UserAgent;
#读取about_shenji.file文件的信息
open FH,"../about_shenji.conf" or die $!;
local $/=undef;
$scalar = <FH>;
close FH;
#print $scalar."11111";
my $all_xingxi=$scalar;
my @all=split(/\n/,$all_xingxi);
my $all_way=$all[0];
my $all_status=$all[1];
my $all_version=$all[2];

my @all_way_d=split(/=/,$all_way);
my $way=$all_way_d[1];#升级方式

my @all_status_d=split(/=/,$all_status);#升级状态
my $status=$all_status_d[1];

my @all_version_d=split(/=/,$all_version);#版本号
my $version=$all_version_d[1];

my $sj_version=$version+0.01;
my $sj_filename=$sj_version."_update.tar.gz";
# #获取机器码
 # my $lic;
 # open($lic,"/usr/sniffer/engine_code.txt");
# $lic=<$lic>;

##获取升级中心的地址
my $url_upload;
open($url_upload,"/var/www/html/upload/about_url.conf");
$url_upload=<$url_upload>;

 if($way==0){#自动升级需要去下载升级包
	 if($status!=1){#没有正在升级的升级包,可以去下载
	    my $url="$url_upload/interface/interface.php?action=read&licesneID=升级测试ID&type=shenji&updateID=$version";
		print $url;
	    #下载升级包
		my $newname="/var/www/html/upload/shenjibao/$sj_filename";
		my ($ua, $request , $response , $content);
		$ua = LWP::UserAgent->new;
		$request = HTTP::Request->new('GET',$url);
		$response = $ua->request($request);
		print "2.  ",$response->status_line,"\n";
		print "3.  ",$response->code,"\n";
		print "4.  ",$response->message,"\n";
		$content = $response->content;
	    if($response->code=='200'){#下载成功
		     if(open (my $fh, '>', $newname)){
				 syswrite($fh,$content);
				 print $fh ;#$content;
				 close $fh;
		     }
		 #改变about_shenji.conf中的status状态(变为1,说明可以升级了)
		  open($filevar,">/var/www/html/upload/about_shenji.conf")|| die("Could not open file");
		  my $xie_ru="way=0\n"."status=1"."\n"."version=$version";
		  print $filevar $xie_ru;
		  close $filevar;
	    }else{
		
		   print "无法升级";
		  
		}
			
	}
   
}
